﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using Bot.Domain.Enum;
using AutoMapper;

namespace Bot.Domain.Categories.Service
{
    public class QuickReplyDomainService : IQuickReplyDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, QuickReply> _repository;
        public IMapper Mapper { get; set; }
        public QuickReplyDomainService(IRepository<Guid, QuickReply> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public QuickReply Create(QuickReply quickReply)
        {
            if (quickReply.Id == Guid.Empty)
            {
                quickReply.Id = Guid.NewGuid();
            }
      
            IQuickReplyItemDomainService itemDomainService = _container.Resolve<IQuickReplyItemDomainService>();
            
            foreach(var item in quickReply.Items)
            {
                item.QuickReplyId = quickReply.Id;
                item.Id = Guid.NewGuid();
                itemDomainService.Create(item);
            }
            return _repository.Create(quickReply);
        }
        public QuickReply Get(Guid id)
        {
            return _repository.Get(id);
        }
        public QuickReply GetById(Guid id, string include = "")
        {
            var quickReply = _repository.Get(id);
            if (quickReply != null)
            {                
                IQuickReplyItemDomainService quickReplyItemDomainService = _container.Resolve<IQuickReplyItemDomainService>();
                quickReply.Items = quickReplyItemDomainService.GetByQuickReplyId(quickReply.Id, include).ToList();                
            }
            return quickReply;
        }

        public QuickReply Update(QuickReplyUpdateBo bo)
        {
            QuickReply quickReply = _repository.Get(bo.Id);
            quickReply.Type = bo.Type;
            quickReply.Name = bo.Name;
            quickReply.IsDeleted = bo.IsDeleted;

            IQuickReplyItemDomainService quickReplyItemDomainService = _container.Resolve<IQuickReplyItemDomainService>();

            List<Guid> quickReplyItemIds = new List<Guid> { };

            foreach (var item in bo.Items)
            {
                if (item.Id != Guid.Empty)
                {
                    //修改      
                    item.QuickReplyId = bo.Id;
                    quickReplyItemDomainService.Update(item);
                    quickReplyItemIds.Add(item.Id);
                }
                else
                {
                    //新增
                    QuickReplyItem quickReplyItem = Mapper.Map<QuickReplyItem>(item);
                    quickReplyItem.Id = Guid.NewGuid();
                    item.Id = quickReplyItem.Id;
                    quickReplyItem.QuickReplyId = bo.Id;
                    item.QuickReplyId = bo.Id;                    
                    quickReplyItemDomainService.Create(quickReplyItem);
                    quickReplyItemIds.Add(quickReplyItem.Id);
                }
            }
            //删除
            var quickReplyItems = quickReplyItemDomainService.GetByQuickReplyId(bo.Id);
            foreach (var q in quickReplyItems)
            {
                if (!quickReplyItemIds.Contains(q.Id))
                {
                    quickReplyItemDomainService.Delete(q.Id);
                }
            }

            _repository.Update(quickReply);

            return quickReply;
        }

        public IEnumerable<QuickReply> List(Guid botId, string include = "")
        {     
            var list = _repository.List(new QuickReplyFilterSpecification(botId));
            if (list != null && list.Count > 0 && !string.IsNullOrEmpty(include) && include.Equals("items", StringComparison.OrdinalIgnoreCase))
            {
                IQuickReplyItemDomainService quickReplyItemDomainService = _container.Resolve<IQuickReplyItemDomainService>();
                foreach(var quickReply in list)
                {
                    quickReply.Items = quickReplyItemDomainService.GetByQuickReplyId(quickReply.Id).ToList();
                }
            }
            return list;
        }

        public IEnumerable<QuickReply> List(Guid botId, EnumReplyType type, string include = "")
        {
            var list = _repository.List(new QuickReplyFilterSpecification(botId, type));
            if (list != null && list.Count > 0 && !string.IsNullOrEmpty(include) && include.Equals("items", StringComparison.OrdinalIgnoreCase))
            {
                IQuickReplyItemDomainService quickReplyItemDomainService = _container.Resolve<IQuickReplyItemDomainService>();
                foreach (var quickReply in list)
                {
                    quickReply.Items = quickReplyItemDomainService.GetByQuickReplyId(quickReply.Id).ToList();
                }
            }
            return list;
        }

        public QuickReply GetByName(Guid botId, string name)
        {
            return _repository.List(new QuickReplyFilterSpecification(botId, name)).First();
        }

        public void Delete(Guid id)
        {
            QuickReply quickReply = GetById(id, "items");

            if(quickReply.Items != null
                && quickReply.Items.Count >0 ) 
            {
                IQuickReplyItemDomainService quickReplyItemDomainService = _container.Resolve<IQuickReplyItemDomainService>();
                foreach(var item in quickReply.Items)
                {
                    quickReplyItemDomainService.Delete(item.Id);
                }
            }

            _repository.Delete(quickReply);

        }

        public IEnumerable<QuickReply> GetByBotId(Guid botId, string include = "")
        {
            var quickReplys = _repository.List(new QuickReplyFilterSpecification(botId));
            if(quickReplys!=null
                && quickReplys.Count() >0 
                && !string.IsNullOrEmpty(include)
                && include.Split(',').Contains("items"))
            {
                IQuickReplyItemDomainService quickReplyItemDomainService = _container.Resolve<IQuickReplyItemDomainService>();
                foreach (var quickReply in quickReplys)
                {                    
                    quickReply.Items = quickReplyItemDomainService.GetByQuickReplyId(quickReply.Id, include).ToList();
                }
            }
            return quickReplys;
        }
    }
}
